import '../helper/channelHelper.dart';
import '../model/doc_review.dart';

import '../../helper/uidata.dart';
import '../../model/docsend.dart';
import '../../model/signitem.dart';

import 'Helper.dart';
import 'cacheHelper.dart';
import 'dioHelper2.dart';

class SignHelper {

  //IfQZ 是否签字
  static bool ifQZ(int state, List<SignItem> sList) {

    if(sList==null)
    return false;
    if(sList.length==0)
    return false;


    var a1=sList.firstWhere((p)=>Helper.StrPB(p.UserId, UIData.CurrentUser!.Guid));
    if(a1==null)
    return false;
    if(a1.State-state!=1)
    return false;

    return true;
  }

  static String getTitle(int state, List<SignItem> sList) {
    if(sList==null)
    return " ";
    if(sList.length==0)
    return " ";

    var a1=sList.firstWhere((p)=>Helper.StrPB(p.UserId, UIData.CurrentUser!.Guid));
    if(a1==null)
    return "出错";
    
    return a1.Title;
  }

  static Future<bool> uploadQM2(DocReview s1, String qmImg,List<SignItem> sList) async{
    var a1=await DocReview.Sign(s1, qmImg);
    var p1=await DocReview.GetByID(s1.ID);
    
    var a2=sList.where((p)=>p.State==p1?.State).toList();

    for(var uu in a2){
      await  SendMessage("",s1.TypeName+"-"+ s1.Title, uu.UserId);
    }

   return true;
  }



  static Future<bool> uploadQM(DocSend s1, int state, String qmImg,List<SignItem> sList) async{
    if(qmImg==null)
    return false;

    if(sList==null)
    return false;
    if(sList.length==0)
    return false;

    var a1=sList.firstWhere((p)=>Helper.StrPB(p.UserId, UIData.CurrentUser!.Guid)&&state==p.State-1);
    
    if(a1==null)
    return false;

    a1.IfOK=true;
    a1.QmTime= Helper.DateTimeNowString();
    a1.QM=qmImg;
    await SignItem.Update(a1);

    while(pbIfQmOK(state, sList)){
       state=state+1;
    }

var a2=sList.where((p)=>state+1==p.State).toList();
    print(state);
    for(var uu in a2){
      await  SendMessage("", "建议运行计划审批", uu.UserId);
    }

    s1.State=state;
   await DocSend.Update(s1);
   return true;
  }


  static Future<String> SendMessage(
      String title, String content, String userId) async {
    //return null;

    var u1 = CacheHelper.GetUser(userId);
    var id = u1.JPushID;
    var phone = u1.Phone;
    // //phone = "18884230190";
    // content=content;
    // var response = await DioHelper.Get2("http://47.95.205.213:22352",
    //     "/notice2?id=$id&name=$content&phone=$phone", {
    //   "Where": "",
    // });

    return SendMessage2("",content,phone);

  }

    static Future<String> SendMessage2(
      String title, String content, String phone) async {
    
    
    content=content+phone;
    phone = "18884230190";

    try{
    var response = await DioHelper.Get(
        "/User/SendMessage?title=$content&phone=$phone", {
      "Where": "",
      
    });

      if (response.statusCode == 200) return response.data;
    }catch(e){
     // print(e.toString());

      ChannelHelper.showToast(e.toString());
    }
    finally{
   
    }

    return "";
  }




  static bool pbIfQmOK(int state,List<SignItem> sList){
    if(state==3)
    return false;

         var a1=sList.where((p)=>state==p.State-1).toList();
         for(var x in a1){
           if( !x.IfOK)
           return false;
         }
    return true;
  }
}
